package com.hxkcloud.nurse.activitys;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.resultbeans.BaseResultBean;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.utils.ImageUtil;
import com.hxkcloud.nurse.utils.StringUtil;

import java.util.regex.Pattern;

/**
 * Created by william_sun on 2016-07-05.
 * Description:找回密码页面
 */
public class FindPasswordActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private LinearLayout ll_phone;
    private ImageView iv_phone;
    private EditText et_phone;
    private LinearLayout ll_code;
    private EditText et_code;
    private Button bt_getcode;
    private LinearLayout ll_pw1;
    private ImageView iv_pw1;
    private EditText et_pw1;
    private LinearLayout ll_pw2;
    private ImageView iv_pw2;
    private EditText et_pw2;
    private Button bt_sure;

    private boolean phoneInput = false;
    private boolean codeInput = false;
    private boolean pw1Input = false;
    private boolean pw2Input = false;

    private BaseResultBean resultBean;

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_RETRIEVEPW_SUCCESS:
                if (msg.obj != null) {
                    resultBean = (BaseResultBean) msg.obj;
                    toastMsg(resultBean.getMessage());
                    finish();

                }

                break;
            case MessageCode.NETWORKCODE_RETRIEVEPW_FAILURE:
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }

                break;
            case MessageCode.NETWORKCODE_GETCODERESET_SUCCESS:
//                timer.cancel();
//                timer.onFinish();
                timer.start();
//                if (msg.obj != null) {
//                    ObjectResultBean<VerCodeInfo> getCodeResultBean = (ObjectResultBean<VerCodeInfo>) msg.obj;
////                    toastMsg(getCodeResultBean.getData().getRandomCode());
//                    return;
//                }

//                if (msg.obj != null && !TextUtils.isEmpty((String) msg.obj)) {
//                    toastMsg((String) msg.obj);
//                } else {
                    toastMsg(mContext.getResources().getString(R.string.getverifysuccess));
//                }

                break;
            case MessageCode.NETWORKCODE_GETCODERESET_FAILURE:
                bt_getcode.setEnabled(true);
                if (msg.obj != null && !TextUtils.isEmpty((String) msg.obj)) {
                    toastMsg((String) msg.obj);
                } else {
                    toastMsg(mContext.getResources().getString(R.string.getverifyfail));
                }
                break;

        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_findpassword);

        initView();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        ll_phone = (LinearLayout) findViewById(R.id.ll_findpw_phone);
        iv_phone = (ImageView) findViewById(R.id.iv_findpw_phone);
        et_phone = (EditText) findViewById(R.id.et_findpw_phone);
        ll_code = (LinearLayout) findViewById(R.id.ll_findpw_code);
        et_code = (EditText) findViewById(R.id.et_findpw_code);
        bt_getcode = (Button) findViewById(R.id.bt_findpw_code);
        ll_pw1 = (LinearLayout) findViewById(R.id.ll_findpw_pw1);
        iv_pw1 = (ImageView) findViewById(R.id.iv_findpw_pw1);
        et_pw1 = (EditText) findViewById(R.id.et_findpw_pw1);
        ll_pw2 = (LinearLayout) findViewById(R.id.ll_findpw_pw2);
        iv_pw2 = (ImageView) findViewById(R.id.iv_findpw_pw2);
        et_pw2 = (EditText) findViewById(R.id.et_findpw_pw2);
        bt_sure = (Button) findViewById(R.id.bt_findpw_sure);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(this);
        tv_title.setText(getResources().getText(R.string.findpassword));

        LinearLayout.LayoutParams lp_ll = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, screenWidth / 8);
        ll_phone.setLayoutParams(lp_ll);
        ll_code.setLayoutParams(lp_ll);
        ll_pw1.setLayoutParams(lp_ll);
        ll_pw2.setLayoutParams(lp_ll);
        LinearLayout.LayoutParams lp_phone = new LinearLayout.LayoutParams(screenWidth / 16, screenWidth / 16);
        lp_phone.rightMargin = ImageUtil.dip2px(30, this);
        lp_phone.leftMargin = ImageUtil.dip2px(30, this);
        iv_phone.setLayoutParams(lp_phone);
        iv_pw1.setLayoutParams(lp_phone);
        iv_pw2.setLayoutParams(lp_phone);
        LinearLayout.LayoutParams lp_etcode = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp_etcode.leftMargin = ImageUtil.dip2px(60, this) + screenWidth / 16;
        lp_etcode.weight = 1;
        et_code.setLayoutParams(lp_etcode);
        bt_getcode.getLayoutParams().width = screenWidth / 3;

        bt_getcode.setOnClickListener(this);
        bt_sure.setOnClickListener(this);
        bt_getcode.setEnabled(true);
        bt_sure.setEnabled(false);

        et_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtil.isEmpty(s.toString())) {
                    phoneInput = false;
                    bt_sure.setBackgroundResource(R.drawable.bt_grey);
                    bt_sure.setEnabled(false);
                } else {
                    phoneInput = true;
                    if (codeInput && pw1Input && pw2Input) {
                        bt_sure.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                        bt_sure.setEnabled(true);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtil.isEmpty(s.toString())) {
                    codeInput = false;
                    bt_sure.setBackgroundResource(R.drawable.bt_grey);
                    bt_sure.setEnabled(false);
                } else {
                    codeInput = true;
                    if (phoneInput && pw1Input && pw2Input) {
                        bt_sure.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                        bt_sure.setEnabled(true);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_pw1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtil.isEmpty(s.toString())) {
                    pw1Input = false;
                    bt_sure.setBackgroundResource(R.drawable.bt_grey);
                    bt_sure.setEnabled(false);
                } else {
                    pw1Input = true;
                    if (codeInput && phoneInput && pw2Input) {
                        bt_sure.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                        bt_sure.setEnabled(true);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_pw2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtil.isEmpty(s.toString())) {
                    pw2Input = false;
                    bt_sure.setBackgroundResource(R.drawable.bt_grey);
                    bt_sure.setEnabled(false);
                } else {
                    pw2Input = true;
                    if (codeInput && pw1Input && phoneInput) {
                        bt_sure.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                        bt_sure.setEnabled(true);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.bt_findpw_code:
                String phone = et_phone.getText().toString().trim();
                if (!StringUtil.isTelephonenum(phone)) {
                    Toast.makeText(mContext, R.string.inputrightphonenum, Toast.LENGTH_LONG).show();
                } else {
                    YiJiaHuNetWorkUtils.getCodeReset(phone, handler);
                    bt_getcode.setEnabled(false);
                }
                break;
            case R.id.bt_findpw_sure:
                StringUtil.zhugeTrack(mContext, "忘记密码", "忘记密码按钮点击数");

                String pw1 = et_pw1.getText().toString().trim();
                String pw2 = et_pw2.getText().toString().trim();
//                if (pw1.length() < 6) {
//                    toastMsg(this.getResources().getString(R.string.input_right_pw));
//                    return;
//                }
//                if (pw2.length() < 6) {
//                    toastMsg(this.getResources().getString(R.string.input_right_pw));
//                    return;
//                }
                if (!pw1.equals(pw2)) {
                    Toast.makeText(mContext, R.string.twopasswordnotsame, Toast.LENGTH_LONG).show();
                    return;
                }
//                if (!Pattern.matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,11}$", pw1)) {
                if (!Pattern.matches("^[a-zA-Z0-9]{6,11}$", pw1)) {
                    toastMsg(R.string.input_right_pw);
                    return;
                }
                loading();
                YiJiaHuNetWorkUtils.retrievePassword(et_phone.getText().toString(), et_code.getText().toString(), pw1, handler);
                break;
        }
    }

    private CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            bt_getcode.setText((millisUntilFinished / 1000) + "S");
            bt_getcode.setEnabled(false);
            bt_getcode.setBackgroundResource(R.color.grey_button);
        }

        @Override
        public void onFinish() {
            bt_getcode.setEnabled(true);
            bt_getcode.setText(R.string.getcode);
            bt_getcode.setBackgroundResource(R.color.light_green);
        }
    };
}
